#include"MyPlayer.h"
FF_STATUS g_ff_status;
int myplayer(const char*filename){	
    SDL_Thread*PlayThread=NULL;
    SDL_Thread*DecThread=NULL;
    memset(&g_ff_status,0,sizeof(FF_STATUS));
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        exit(-1);
    }
    g_ff_status.running=1;
    InitSDLMutx();
    av_log_set_level(AV_LOG_VERBOSE);
    if(InitFFMpeg(filename)!=0){
        printf("InitFFMpeg Err\n");
        exit(-1);
    }else{
        printf("InitFFMpeg Ok usevideothread:%d useaudiothread:%d\n",g_ff_status.hasvideo,g_ff_status.hasaudio);
    }
    if(g_ff_status.hasvideo) {
        PlayThread=SDL_CreateThread(SDL_VIDEO_Thread,NULL);
        if(PlayThread==NULL){
            g_ff_status.hasvideo=0;
            printf("CreateVideoThread Failed\n");
        }else{
            printf("CreateVideoThread OK\n");
        }
    }
    if(g_ff_status.hasaudio) {
        InitAudioThread(g_ff_status.sample_rate,g_ff_status.samples);
        SDL_PauseAudio(0);
    }
    DecThread=SDL_CreateThread(FF_DEC_Thread,NULL);
    if(DecThread==NULL){
       printf("CreateThread Failed\n");
    }	
///////////////////////////////////////////
    while(g_ff_status.running) {
        SDL_Event event={0};
        SDL_PollEvent(&event);
        if(event.type==SDL_QUIT){
            printf("Recv Quit Single\n"); 
            g_ff_status.running=0;
        }
    }
    g_ff_status.running=0;;
    if(DecThread){
        SDL_WaitThread(DecThread,NULL);
    }
    if(PlayThread){
        SDL_WaitThread(PlayThread,NULL);
    }
    UnInitSDLMutx();
    UnInitFFMpeg();
    SDL_CloseAudio();
    SDL_Quit();
	return 0;
}